<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>创建对象的多种方式</title>
	</head>
	<body>
		<script type="text/javascript">
		//1.通过字面量来创建对象
			/* var student = {
				name:"子明",
				age:22,
				school:"小圣贤庄",
				like:function(){
					console.log(this.name+"喜欢吃烧鸡");
				}
			};
 */


		//2.工厂模式
		/* function Student(name,age,school){
		var student = {
			name: name,
			age: age,
			school:school,
			like:function(){
				console.log(this.name+"喜欢吃烧鸡！");
			}
		};
		return student;
		}

		var a = Student("子明",22,"小圣贤庄");
		console.log(a); */
			


		//3.通过class创建对象
 		/* class student{
			constructor(name,age,likename){
				this.name=name;
				this.age= age;
				this.likename = likename;
			   // this.like= function(){
			   //	console.log(this.name+"喜欢吃烤鸡！");
			   //}; 
			};
			like(){
				console.log(this.name+"喜欢"+this.likename);    //自动设置原型对象属性
			}
		}

		var b1 = new student("子羽", 23, "虞姬");
		var b2 = new student("子明", 18, "月儿");
		console.log(b1);
		console.log(b2); */



		//4、(一)自定义构造函数创建对象
		/* function student(name,age,likename){
			this.name = name;
			this.age = age;
			this.like= function(){
				console.log(this.name+"喜欢"+likename);
			};
			
		};
		var c1= new student("子羽",23,"虞姬");
		var c2 = new student("子明",18,"月儿");
		console.log(c1);
		console.log(c2); */

		//4、（二）完整自定义构造函数创建对象
		//1.创建自定义构造函数
		//2.创建原型对象
		//3.设置构造函数原型对象属性protoType

		function student(name, age, likename) {
				this.name = name;
				this.age = age;
				this.likename= likename;
			};                              //1

		var studentproto = {
			like:function(){
				console.log(this.name+"喜欢"+this.likename);
			},
			is:function(){
				console.log(this.name+"属于小圣贤庄");
			}
		};                                  //2

		student.prototype = studentproto;	//3

		var d1 = new student("子羽", 23, "虞姬");
		var d2 = new student("子明", 18, "月儿");
		console.log(d1);
		console.log(d2);                   


		</script>
	</body>
</html>